package StringComparision;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import CA.CrashSuper;
import CA.FileInfo;
import CA.Helper;

import junit.framework.TestCase;

public class StringDistancesTest extends TestCase {
	private final String MainExaminLocation = System.getProperty("user.dir") + "/UnitTestData/";
	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	// Matching Lines in Files

	public void testGetMatchingLinesInFile()
	{
		String compareFile = MainExaminLocation+"AndyDistanceTest/Folder1/";
		String compareFile2 = MainExaminLocation+"AndyDistanceTest/";
		StringDistances sd = new StringDistances();
		

		CrashSuper cs = new CrashSuper();
		Helper h = new Helper();
		List<FileInfo> fileList = new ArrayList<FileInfo>();	
		fileList=cs.getFileList(compareFile2);
		cs.BuildFileData(fileList, compareFile2, false);
		
		// Create the two test files
		File f1=new File(compareFile+"file1.txt");
		File f2=new File(compareFile+"file2.txt");
		File f3=new File(compareFile+"file3.txt");
		File f4=new File(compareFile+"file4.txt");
		File f5=new File(compareFile+"file5.txt");
		File f6=new File(compareFile+"file6.txt");

		assertEquals(3,sd.getMatchingLinesInFile(f1, f2));
		assertEquals(0,sd.getMatchingLinesInFile(f1, f3));
		assertEquals(4,sd.getMatchingLinesInFile(f1, f4));
		assertEquals(3,sd.getMatchingLinesInFile(f2, f4));
		assertEquals(0,sd.getMatchingLinesInFile(f2, f3));
		assertEquals(0,sd.getMatchingLinesInFile(f3, f4));
		assertEquals(3,sd.getMatchingLinesInFile(f1, f5));
		assertEquals(3,sd.getMatchingLinesInFile(f1, f6));
		assertEquals(3,sd.getMatchingLinesInFile(f2, f5));
		assertEquals(0,sd.getMatchingLinesInFile(f3, f5));
		assertEquals(3,sd.getMatchingLinesInFile(f4, f5));
		assertEquals(3,sd.getMatchingLinesInFile(f2, f6));
		assertEquals(0,sd.getMatchingLinesInFile(f3, f6));
		assertEquals(3,sd.getMatchingLinesInFile(f4, f6));
		assertEquals(20,sd.getMatchingLinesInFile(f5, f6));
		
		assertEquals(1,fileList.size()); // One folder exists - May change as more files are added
		assertEquals(48,fileList.get(0).getTotalLinesSameInFolder());
		
		assertEquals(.67,fileList.get(0).getAvgByLineTotalLinesSameInFolder());
		assertEquals(8.0,fileList.get(0).getAvgByCrashTotalLinesSameInFolder());
		
	
		// Now test the average Longest string sequences

		
		assertEquals(.5,fileList.get(0).getAvgByLineTotalLongestLineSameCountInFolder());
		assertEquals(6.0,fileList.get(0).getAvgByCrashLongestLineSameCountInFolder());
		
		
	}


	// BrodieLCS
	public void testGetBrodieLCS()
	{
		StringDistances sd = new StringDistances();
		Helper h = new Helper();
		CrashSuper cs = new CrashSuper();
		String compareFile = MainExaminLocation+"BrodieTest/Folder1/";
		String compareFile2 = MainExaminLocation+"BrodieTest/";
		String val1 = "f1@!@f2@!@f3@!@f4@!@";
		String val2 = "f1@!@f2@!@f3@!@f4@!@";
		String val3 = "f1@!@f2@!@f3@!@f4@!@f5@!@";
		String val4="f0@!@f1@!@f2@!@f3@!@f5@!@";
		String val5="f1@!@f2@!@f3@!@f4@!@f5@!@";
		String val6="f1@!@f2@!@f3@!@f4@!@f6@!@f7@!@f8@!@";
		
		String tst1 = "f3";
		String tst2 = "f2";
		String tst3 = "f3@!@";
		String tst4 = "f2@!@";
		String tst5 = "@!@f2@!@";
		String tst6 = "@!@f2@!@";
		
		File f1=new File(compareFile+"file1.txt");
		File f2=new File(compareFile+"file2.txt");
		File f3=new File(compareFile+"file3.txt");
		File f4=new File(compareFile+"file4.txt");
		File f5=new File(compareFile+"file5.txt");
		File f6=new File(compareFile+"file6.txt");
		
		File af1=new File(compareFile+"af1.txt");
		File af1a=new File(compareFile+"af1a.txt");
		File af2=new File(compareFile+"af2.txt");
		File af3=new File(compareFile+"af3.txt");
		

		
		// now test out the actual files pulled in
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1, f2));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1, f3));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f1, f4));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1, f5));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1, f6)); //good up to here including this
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f2, f3));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f2, f4));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f2, f5));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f2, f6));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f3, f4));
		assertEquals(5,sd.getNumberBrodieSubStringMatchesByFile(f3, f5));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f3, f6));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f4, f5));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f4, f6));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f5, f6));
		
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1,f2));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1,f3));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f1,f4));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1,f5));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f1,f6));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f2,f3));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f2,f4));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f2,f5));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f2,f6));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f3,f4));
		assertEquals(5,sd.getNumberBrodieSubStringMatchesByFile(f3,f5));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f3,f6));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f4,f5));
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(f4,f6));
		assertEquals(4,sd.getNumberBrodieSubStringMatchesByFile(f5,f6));
		
		
		
		// getBrodieDistance
		// some have been tested out
		assertEquals(1.0,sd.getBrodieDistance(f1, f2));
		assertEquals(1.0,sd.getBrodieDistance(f1, f3));
		assertEquals(.75,sd.getBrodieDistance(f1, f4));
		assertEquals(1.0,sd.getBrodieDistance(f1, f5));
		assertEquals(.8,sd.getBrodieDistance(f1, f6));
		assertEquals(1.0,sd.getBrodieDistance(f2, f3));
		assertEquals(.75,sd.getBrodieDistance(f2, f4));
		assertEquals(1.0,sd.getBrodieDistance(f2, f5));
		assertEquals(.8,sd.getBrodieDistance(f2, f6));
		assertEquals(.6,sd.getBrodieDistance(f3, f4));
		assertEquals(1.0,sd.getBrodieDistance(f3, f5));
		assertEquals(.67,sd.getBrodieDistance(f3, f6));
		assertEquals(.6,sd.getBrodieDistance(f4, f5));
		assertEquals(.5,sd.getBrodieDistance(f4, f6));
		assertEquals(.67,sd.getBrodieDistance(f5, f6));
	
		
		
		
	
		
		
		
		assertEquals(3,sd.getNumberBrodieSubStringMatchesByFile(af1,af2));
		assertEquals(5,sd.getNumberBrodieSubStringMatchesByFile(af1,af1a));
		assertEquals(0,sd.getNumberBrodieSubStringMatchesByFile(af1,af3));
		
		
		// BrodieDistanceWithLineConsiderdations
		
		
		
	}

	public void testGetLineDistanceWithMatchPosistionConsideration()
	{
		StringDistances sd = new StringDistances();
		Helper h = new Helper();
		// CrashSuper cs = new CrashSuper();
		String compareFile = MainExaminLocation+"BrodieTest2/Folder1/";
		
		File f1=new File(compareFile+"file1.txt");
		File f2=new File(compareFile+"file2.txt");
		File f3=new File(compareFile+"file3.txt");
		File f4=new File(compareFile+"file4.txt");
		File f5=new File(compareFile+"file5.txt");
		File f6=new File(compareFile+"file6.txt");
		
		assertEquals(1.0,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f4, f4));
		
		assertEquals(1.0,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f1, f2));
		assertEquals(.715,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f1, f3));	
		assertEquals(0.0,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f1, f4));	
		assertEquals(.715,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f1, f5));	
		assertEquals(.455,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f1, f6));	
		assertEquals(.715,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f2, f3));	
		assertEquals(0.0,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f2, f4));	
		assertEquals(.715,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f2, f5));	
		assertEquals(.455,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f2, f6));	
		assertEquals(.065,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f3, f4));	
		assertEquals(1.0,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f3, f5));	
		assertEquals(.56,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f3, f6));	
		assertEquals(.065,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f4, f5));
		assertEquals(0.0,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f4, f6));	
		assertEquals(.56,sd.getBrodieLineDistanceWithMatchPosistionConsideration(f5, f6));	
		


		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f1, f2));
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f1, f3));
		assertEquals(0,sd.getMaxNumberMatchingLineSequenceInFiles(f1, f4));
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f1, f5));
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f1, f6));
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f2, f3));
		assertEquals(0,sd.getMaxNumberMatchingLineSequenceInFiles(f2, f4));
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f2, f5));
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f2, f6));
		assertEquals(1,sd.getMaxNumberMatchingLineSequenceInFiles(f3, f4));
		assertEquals(5,sd.getMaxNumberMatchingLineSequenceInFiles(f3, f5));	
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f3, f6));
		assertEquals(1,sd.getMaxNumberMatchingLineSequenceInFiles(f4, f5));
		assertEquals(0,sd.getMaxNumberMatchingLineSequenceInFiles(f4, f6));
		assertEquals(4,sd.getMaxNumberMatchingLineSequenceInFiles(f5, f6));



	}
	

	public void testBrodieValues()
	{
	
		String compareFile2 = MainExaminLocation+"BrodieTest2/";
		StringDistances sd = new StringDistances();
		

		CrashSuper cs = new CrashSuper();
		Helper h = new Helper();
		List<FileInfo> fileList = new ArrayList<FileInfo>();	
		fileList=cs.getFileList(compareFile2);
		cs.BuildFileData(fileList, compareFile2, false);
		
		
		assertEquals(56, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieLCS());
		assertEquals(43, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieMatches());
		assertEquals(12.14, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieValue());
		assertEquals(7.02, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieValueWithLineWeight());
		assertEquals(30, fileList.get(h.getArrayIndex(fileList,"folder1")).getTotalNumberofLines());
		
		
		assertEquals(9.33, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieLCSValueByFile());
		assertEquals(1.87, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieLCSValueByLine());
		
		assertEquals(7.17, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieMatchesByFile());
		assertEquals(1.43, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieMatchesByLine());
		
		assertEquals(2.02, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieValueByFile());
		assertEquals(.4, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieValueByLine());
		
		assertEquals(1.17, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieValueLineWeightByFile());
		assertEquals(.23, fileList.get(h.getArrayIndex(fileList,"folder1")).getBrodieValueLineWeightByLine());
		
		
		
		//test with multiple folders > make sure all unit tests pass first
		assertEquals(56, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieLCS());
		assertEquals(43, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieMatches());
		assertEquals(12.14, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieValue());
		assertEquals(7.02, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieValueWithLineWeight());
		assertEquals(30, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getTotalNumberofLines());
		
		
		assertEquals(9.33, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieLCSValueByFile());
		assertEquals(1.87, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieLCSValueByLine());
		
		assertEquals(7.17, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieMatchesByFile());
		assertEquals(1.43, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieMatchesByLine());
		
		assertEquals(2.02, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieValueByFile());
		assertEquals(.4, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieValueByLine());
		
		assertEquals(1.17, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieValueLineWeightByFile());
		assertEquals(.23, fileList.get(h.getArrayIndex(fileList,"Copy of folder1")).getBrodieValueLineWeightByLine());
		
	
		// test folder 3
		assertEquals(37, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieLCS());
		assertEquals(27, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieMatches());
		assertEquals(7.59, h.roundDouble(fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieValue()));
		assertEquals(4.14, h.roundDouble(fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieValueWithLineWeight()));
		assertEquals(30, fileList.get(h.getArrayIndex(fileList,"Diff")).getTotalNumberofLines());
		
		
		assertEquals(6.17, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieLCSValueByFile());
		assertEquals(1.23, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieLCSValueByLine());
		
		assertEquals(4.5, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieMatchesByFile());
		assertEquals(.9, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieMatchesByLine());
		
		assertEquals(1.26, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieValueByFile());
		assertEquals(.25, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieValueByLine());
		
		assertEquals(.69, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieValueLineWeightByFile());
		assertEquals(.14, fileList.get(h.getArrayIndex(fileList,"Diff")).getBrodieValueLineWeightByLine());
		
		
		// now test the average calculations
		
		//AVG Matches for file
		assertEquals(6.28, sd.getAvgTotalMatchesByFile(fileList));  //former value:37.67
		
		//AVG Brodie matches
		assertEquals(1.77, sd.getAvgBrodieByFile(fileList)); //former value: 10.62
		
		// AVG Brodie with BrodiceLCS
		assertEquals(8.28, sd.getAvgBrodieLCSByFile(fileList)); //former value: 49.67
		
		// AVG Brodie byWeight
		assertEquals(1.01, sd.getAvgBrodieByWeight(fileList)); //former value: 6.06
		

		
		
		
		
	}
	
	
	
	
	

	public void testAbsoluteLCS()
	{		
		String compareFile = MainExaminLocation+"BrodieTest/Folder1/";
		String compareFile2 = MainExaminLocation+"BrodieLCS/";
		StringDistances sd = new StringDistances();
		
		File af1=new File(compareFile+"af1.txt");
		File af1a=new File(compareFile+"af1a.txt");
		File af2=new File(compareFile+"af2.txt");
		File af3=new File(compareFile+"af3.txt");
		File f1=new File(compareFile+"file1.txt");
		File f2=new File(compareFile+"file2.txt");
		File f3=new File(compareFile+"file3.txt");
		File f4=new File(compareFile+"file4.txt");
		File f5=new File(compareFile+"file5.txt");
		File f6=new File(compareFile+"file6.txt");
		
		assertEquals(1.0,sd.getAbsoluteLCS(af1, af1a));
		assertEquals(1.0,sd.getAbsoluteLCS(af1, af1));
		assertEquals(.6,sd.getAbsoluteLCS(af1, af2));
		assertEquals(0.0,sd.getAbsoluteLCS(af1, af3));
		assertEquals(0.0,sd.getAbsoluteLCS(af2, af3));
		assertEquals(.89,sd.getAbsoluteLCS(f2, f5));
		assertEquals(.89,sd.getAbsoluteLCS(f2, f3));
		assertEquals(.67,sd.getAbsoluteLCS(f2, f4));
		
		
		
		// pull in the file values from the second brodie location
		File file1=new File(compareFile2+"BrodieLCS/file1.txt");
		File file2=new File(compareFile2+"BrodieLCS/file2.txt");
		File file3=new File(compareFile2+"BrodieLCS/file3.txt");
		File file4=new File(compareFile2+"BrodieLCS/file4.txt");
		
		// test out the individual files to make sure the averages will be the same
		
		assertEquals(1.0,sd.getAbsoluteLCS(file1, file1));
		assertEquals(1.0,sd.getAbsoluteLCS(file1, file2));
		assertEquals(.89,sd.getAbsoluteLCS(file1, file3));
		assertEquals(.89,sd.getAbsoluteLCS(file1, file4));
		assertEquals(.89,sd.getAbsoluteLCS(file2, file3));
		assertEquals(.89,sd.getAbsoluteLCS(file2, file4));
		assertEquals(1.0,sd.getAbsoluteLCS(file3, file4));
		
		
		// Now test with loading the files into an array
		CrashSuper cs = new CrashSuper();
		Helper h = new Helper();
		List<FileInfo> fileList = new ArrayList<FileInfo>();	
		fileList=cs.getFileList(compareFile2);
		cs.BuildFileData(fileList, compareFile2, false);
	
		assertEquals(5.5600000000000005, fileList.get(0).getAbsoluteLCS());  

		
		// put this test in to piggyback on the earlier test setup
		
		assertEquals(5.56,sd.getAvgLCSBYAbsoluteLength(fileList)); //was .31
		
	}



	
	
	public void testCleanBrodieLCS(){
		StringDistances sd = new StringDistances();
		Helper h = new Helper();
		CrashSuper cs = new CrashSuper();
		String compareFile = MainExaminLocation+"BrodieTest/Folder1/";
		

		File f1=new File(compareFile+"file1.txt");
		File f2=new File(compareFile+"file2.txt");
		File f3=new File(compareFile+"file3.txt");
		File f4=new File(compareFile+"file4.txt");
		File f5=new File(compareFile+"file5.txt");
		File f6=new File(compareFile+"file6.txt");
		
		File af1=new File(compareFile+"af1.txt");
		File af1a=new File(compareFile+"af1a.txt");
		File af2=new File(compareFile+"af2.txt");
		File af3=new File(compareFile+"af3.txt");
		
		
		
		assertEquals("@!@l2@!@l3@!@l4@!@",sd.cleanBrodieLCS("Dan@!@l2@!@l3@!@l4@!@", af1, af1a));
		assertEquals("@!@l2@!@l3@!@l4@!@",sd.cleanBrodieLCS("Dan@!@l2@!@l3@!@l4@!@", af1, af2));
		
		
		
		// Could probably add more tests to this.
	}
	
	
	
	
}
